package com.mathset.business.service.impl;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.ModelAndView;

import com.mathset.bean.MailBox;
import com.mathset.bean.Member;
import com.mathset.business.dao.IMailDao;
import com.mathset.business.dao.IMemberDao;
import com.mathset.business.service.IMailService;
import com.mathset.business.service.IMemberService;
import com.mathset.common.MathSetUtil;

@Service
public class MemberService implements IMemberService{
	
	@Autowired
	private IMemberDao iMemberDao;
	
	@Autowired
	private IMailDao iMailDao;
	
	@Autowired
	private IMailService iMailService;
	
	@Override
	public void addMemberAndMailBox(Member member) {
		long crateTime = new java.util.Date().getTime();
		long expiredTime = crateTime + 30*60*1000;
		member.setRegDate(new Date(crateTime));
		iMemberDao.addMember(member);
		MailBox mailBox = new MailBox();
		mailBox.setEmail(member.getEmail());
		mailBox.setUID(member.getUID());
		mailBox.setCreateTime(new Date(crateTime));
		mailBox.setExpiredTime(new Date(expiredTime));
		mailBox.setToken(MathSetUtil.generateToken());
		iMailDao.addMailBox(mailBox);
		iMailService.sendMail(member.getEmail(), mailBox.getToken());
		
	}

	@Override
	public Member retrieveMemberByUserNameAndPassword(String userName, String password) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("userName", userName);
		map.put("password", password);
		Member member = iMemberDao.retrieveMemberByUserNameAndPassword(map);
		return member;
	}

	@Override
	public Member retrieveMemberByUserNameOrMaidId(String userName, String email) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("userName", userName);
		map.put("email", email);
		Member member = iMemberDao.retrieveMemberByUserNameOrMaidId(map);
		return member;
	}

	@Override
	public boolean isValidRegister(Member member, BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			return false;
		}
		if(!member.getPassword().equals(member.getPassword2())) {
			bindingResult.rejectValue("password2", null, "密码不一致");
			return false;
		}
		Member existedMember = 
				this.retrieveMemberByUserNameOrMaidId(member.getUserName(), member.getEmail());
		if(existedMember != null && member.getUserName().equals(existedMember.getUserName())) {
			bindingResult.rejectValue("existedUserName", null, "用户名已被注册");
			return false;
		} 
		if(existedMember != null && member.getEmail().equals(existedMember.getEmail())) {
			bindingResult.rejectValue("existedMailId", null, "邮箱已被注册");
			return false;
		} 
		return true;
	}
	
	@Override
	public boolean isValidMember(Member member, BindingResult bindingResult) {
		Member existedMember = this.retrieveMemberByUserNameAndPassword(member.getUserName(), member.getPassword());
		if(existedMember == null) {
			bindingResult.rejectValue("invalidUserNameOrPassword", null, "用户名或密码不正确");
			return false;
		}
		member.setEmail(existedMember.getEmail());
		member.setEmailStatus(existedMember.getEmailStatus());
		return true;
	}
	
	@Override
	public Member retrieveMemberByMailAddress(String email) {
		return iMemberDao.retrieveMemberByMailAddress(email);
	}
}
